package com.ymatou.iapi.livebuyer.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.LiveRoomInfo;
import com.ymatou.iapi.livebuyer.parameter.QueryLiveRoomByIdsBean;
import com.ymatou.iapi.livebuyer.service.QueryLiveRoomByIdsCall;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.YMTDateUtil;

/**
 * 根据直播间Id批量查询直播间列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_QueryLiveRoomByIds {
	private static QueryLiveRoomByIdsBean queryliveroombyidsBean;
	private static QueryLiveRoomByIdsCall queryliveroombyidsCall;
	private static Integer liveroomid;
	private static LiveRoomWapper liveRoomWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据直播间Id批量查询直播间列表");
		liveRoomWapper=new LiveRoomWapper();
	}

	@Before
	public void caseUp() {
		queryliveroombyidsBean = new QueryLiveRoomByIdsBean();
		queryliveroombyidsCall = new QueryLiveRoomByIdsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryliveroombyidsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(LiveRoomInfo liveRoomInfo) {
		LiveRoom liveroom = new LiveRoomWapper().selectLiveRoomById(liveRoomInfo.getLiveId());
		Logger.verifyEquals(liveroom.getEndTime().substring(0,19), liveRoomInfo.getEndTime(), "验证EndTime");
		Logger.verifyEquals(liveroom.getStartTime().substring(0,19), liveRoomInfo.getStartTime(), "验证StartTime");
		Logger.verifyEquals(liveroom.getGroupId(), liveRoomInfo.getGroupId(), "验证GroupId");
		Logger.verifyEquals(liveroom.getHistoryPlayUrl(), liveRoomInfo.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(liveroom.getLiveCover(), liveRoomInfo.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(liveroom.getId(), liveRoomInfo.getLiveId(), "验证LiveId");
		// 修改status
		if (liveroom.getLiveStatus() == 0) {
			if (liveroom.getStartTime().compareTo(YMTDateUtil.getSysTime()) < 0) {
				Logger.verifyEquals(1, liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			} else {
				Logger.verifyEquals(liveroom.getLiveStatus(), liveRoomInfo.getLiveStatus(), "验证LiveStatus");
			}
		}

		Logger.verifyEquals(liveroom.getLiveTitle(), liveRoomInfo.getLiveTitle(), "验证LiveTitle");
		Logger.verifyEquals(liveroom.getPlayUrl(), liveRoomInfo.getPlayUrl(), "验证PlayUrl");
		if (liveRoomInfo.getPreVideo() != null) {
			Logger.verifyEquals(liveroom.getPreVideoHeight(), liveRoomInfo.getPreVideo().getPreVideoHeight(), "验证getPreVideoHeight");
			Logger.verifyEquals(liveroom.getPreVideoLength(), liveRoomInfo.getPreVideo().getPreVideoLength(), "验证getPreVideoLength");
			Logger.verifyEquals(liveroom.getPreVideoUrl(), liveRoomInfo.getPreVideo().getPreVideoUrl(), "验证getPreVideoUrl");
			Logger.verifyEquals(liveroom.getPreVideoWidth(), liveRoomInfo.getPreVideo().getPreVideoWidth(), "验证getPreVideoWidth");
		}
		Logger.verifyEquals(liveroom.getUserId(), liveRoomInfo.getUserId(), "验证UserId");
		Logger.verifyEquals(liveroom.getLiveDesc(), liveRoomInfo.getLiveDesc(), "验证LiveDesc");
		Logger.verifyEquals(liveroom.getPutawayCount(), liveRoomInfo.getPutawayCount(), "验证PutawayCount");
		Logger.verifyEquals(liveroom.getAction(), liveRoomInfo.getAction(), "验证Action");
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoomByIds_001() {
		Logger.start(true, "根据直播间Id批量查询直播间列表成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			List<Integer> list = Arrays.asList(liveroomid);
			queryliveroombyidsBean.setLiveIdList(list);
			queryliveroombyidsCall.setData(queryliveroombyidsBean);
			queryliveroombyidsCall.callService();
			Logger.verifyEquals("200", queryliveroombyidsCall.getString("code"), "判断code");
			ArrayList<LiveRoomInfo> liveroominfolist = queryliveroombyidsCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : liveroominfolist) {
				verifyDB(liveRoomInfo);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryLiveRoomByIds_002() {
		Logger.start(true, "不输入LiveId失败");
		try {
			queryliveroombyidsCall.setData(queryliveroombyidsBean);
			queryliveroombyidsCall.callService();
			Logger.verifyEquals("100", queryliveroombyidsCall.getString("code"), "判断code");
			Logger.verifyIsContains("直播Id列表不能为空", queryliveroombyidsCall.getString("message"), "判断message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_QueryLiveRoomByIds_003() {
		Logger.start(true, "输入多个LiveId成功");
		try {
			CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
			if (liveroomid == null) {
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			int liveroomid2=LiveRoomCallService.CreateSellerLive(initcreatebean).getId();
			List<Integer> list = Arrays.asList(liveroomid,liveroomid2);
			queryliveroombyidsBean.setLiveIdList(list);
			queryliveroombyidsCall.setData(queryliveroombyidsBean);
			queryliveroombyidsCall.callService();
			Logger.verifyEquals("200", queryliveroombyidsCall.getString("code"), "判断code");
			ArrayList<LiveRoomInfo> liveroominfolist = queryliveroombyidsCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : liveroominfolist) {
				verifyDB(liveRoomInfo);
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_QueryLiveRoomByIds_004() {
		Logger.start(true, "输入不存在LiveId失败");
		try {	
			List<Integer> list = Arrays.asList(-1);
			queryliveroombyidsBean.setLiveIdList(list);
			queryliveroombyidsCall.setData(queryliveroombyidsBean);
			queryliveroombyidsCall.callService();
			Logger.verifyEquals("200", queryliveroombyidsCall.getString("code"), "判断code");
			Logger.verifyIsContains(0, queryliveroombyidsCall.getLiveRoomInfo().size(), "判断data数组返回为空");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_QueryLiveRoomByIds_005() {
		Logger.start(true, "根据直播间Id批量查询删除直播间列表成功");
		try {
			if (liveroomid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveroomid = livedto.getId();
			}
			liveRoomWapper.updateLiveRoomAction(liveroomid, -1);
			List<Integer> list = Arrays.asList(liveroomid);
			queryliveroombyidsBean.setLiveIdList(list);
			queryliveroombyidsCall.setData(queryliveroombyidsBean);
			queryliveroombyidsCall.callService();
			Logger.verifyEquals("200", queryliveroombyidsCall.getString("code"), "判断code");
			ArrayList<LiveRoomInfo> liveroominfolist = queryliveroombyidsCall.getLiveRoomInfo();
			for (LiveRoomInfo liveRoomInfo : liveroominfolist) {
				verifyDB(liveRoomInfo);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}